<?php

/*
 * Table/TransfertTable.php
 */

namespace RHX\Model\Table;

use RHX\Model\Object\Transfert;
use Zend\Db\Adapter\Adapter;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Select;

class TransfertTable extends AbstractTableGateway {

    protected $table = 'rh_transfert';

    public function __construct(Adapter $adapter) {
	$this->adapter			 = $adapter;
	$this->resultSetPrototype	 = new ResultSet();
	$this->resultSetPrototype->setArrayObjectPrototype(new Transfert($adapter));
	$this->initialize();
    }

    public function getAll() {
	return $this->select();
    }

    public function getAllBySite($site, $type = Transfert::EntrantSortant) {
	return $this->select(function(Select $select) use($site, $type) {
			    $where = new \Zend\Db\Sql\Where();
			    switch ($type) {
				case Transfert::EntrantSortant:
				    $where->equalTo('depart', $site);
				    $where->or->equalTo('arrivee', $site);
				    break;
				case Transfert::Entrant:
				    $where->equalTo('arrivee', $site);
				    break;
				case Transfert::Sortant:
				    $where->equalTo('depart', $site);
				    break;
			    }
			    $select->where($where);
			});
    }

    /**
     * Récupérer une instance de transfert
     *
     * @param int $id
     * @return Transfert
     * @throws \Exception
     */
    public function get($id) {
	$row = $this->select(array('id' => $id))->current();

	if (!$row)
	    throw new \Exception("Introuvable #$id");

	return $row;
    }

    public function erase($id) {
	return $this->delete(array('id' => $id));
    }

    public function save(Transfert $transfert) {
	$data = array(
	    'depart'	 => $transfert->depart,
	    'arrivee'	 => $transfert->arrivee,
	    'matricule'	 => $transfert->matricule,
	    'jour'		 => $transfert->journee,
	    'entree'	 => $transfert->entree,
	    'sortie'	 => $transfert->sortie,
	    'pause'		 => $transfert->pause,
	    'repas'		 => $transfert->repas,
	    'commentaire'	 => $transfert->commentaire,
	);

	$id = (int) $transfert->id;
	if ($id == 0) {
	    $this->insert($data);

	    return $this->lastInsertValue;
	}
	elseif ($this->get($id)) {
	    $this->update($data, array('id' => $id));

	    return $id;
	}
	else {
	    throw new \Exception("Impossible à trouver #$id");
	}
    }

    /**
     * Récupérer les transfert d'une semaine
     *
     * @param string $lundi
     * @param int $site
     * @param int $type
     * @return type
     */
    public function getTransfert($lundi, $site, $type) {
	$dimanche	 = new \DateTime($lundi);
	$dimanche->modify('next sunday');
	$rows		 = $this->select(function(Select $select)use($lundi, $dimanche, $site, $type) {
		    $where = new \Zend\Db\Sql\Where();
		    $where->between('jour', $lundi, $dimanche->format('Y-m-d'));
		    if ($type == 1) {
			// Entrant
			$where->equalTo('arrivee', $site);
		    }
		    else {
			// Sortant
			$where->equalTo('depart', $site);
		    }
		    $select->where($where);
		    $select->order('jour ASC');
		    //var_dump($select->getSqlString());
		});

	return $rows;
    }

    /**
     * Récupérer tous les plannings d'une journée
     *
     * @param string $day
     * @param int $site
     * @param int $type
     * @return array
     */
    public function getTransfertOfOneDay($day, $site, $type) {
	$rows = $this->select(function(Select $select)use($day, $site, $type) {
		    $where = new \Zend\Db\Sql\Where();
		    $where->equalTo('jour', $day);
		    if ($type == Transfert::Entrant) {
			$where->equalTo('arrivee', $site);
		    }
		    elseif ($type == Transfert::Sortant) {
			$where->equalTo('depart', $site);
		    }
		    elseif ($type == Transfert::EntrantSortant) {
			// Entrant Sortant
		    }
		    $select->where($where);
		    $select->order('matricule ASC');
		});

	return $rows;
    }
    
    /**
     * Nombre de personne à une heure précise
     * 
     * @param string $date
     * @param string $heure
     * @param int $site
     * @return array
     */
    public function countPersonne($date, $heure, $site){
	$sql = "SELECT COUNT(*) AS nb FROM rh_transfert WHERE jour = '$date' AND ('$heure' BETWEEN entree AND sortie OR entree = '$heure' OR sortie = '$heure') AND arrivee = $site";
	$result = $this->getAdapter()->query($sql)->execute();
	$current = $result->current();
	return array(
            'value' => (int)$current['nb'],
            'sql' => $sql,
        );
    }

}

?>
